package com.bytz.modules.cms.order.event;

import com.bytz.modules.cms.order.entity.Order;
import com.bytz.modules.cms.order.service.IOrderEmailService;
import com.bytz.modules.cms.order.service.IOrderService;
import com.bytz.modules.cms.order.service.ISalesQuoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class GeSalesQuoteListener {
    @Autowired
    private ISalesQuoteService salesQuote;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderEmailService orderEmailService;


    @Async
    @EventListener
    public void generateSalesQuote(GeSalesQuoteEvent salesQuoteEvent){
        log.info("开始执行报价单监听事件，orderId:{}", salesQuoteEvent.getOrder().getId());
        Order order = salesQuoteEvent.getOrder();
        // 如果order为空则不做处理
        if (order == null){
            return;
        }
        try {
            // 创建报价单以及后续工作
            salesQuote.salesQuote(order);
            // 报价单创建完毕后邮件给 大客户以及对应的销售
            orderEmailService.sendPrepayReminderEmail(order);
        } catch (Exception e) {
            log.error("创建报价单失败，order:" + order.getId(), e);
        }
        log.info("报价单监听事件执行完毕，orderId:{}", salesQuoteEvent.getOrder().getId());
    }
}
